/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.boxin.ims.modules.wechat.web;

import java.io.File;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.utils.FileUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;
import com.boxin.ims.modules.wechat.entity.SeckillConfig;
import com.boxin.ims.modules.wechat.service.SeckillConfigService;
import com.boxin.ims.modules.wechat.utils.WechatUtils;

/**
 * 秒杀配置Controller
 * @author Jakemanse
 * @version 2013-09-28
 */
@Controller
@RequestMapping(value = Global.ADMIN_PATH+"/wechat/seckillConfig")
public class SeckillConfigController extends BaseController {

	@Autowired
	private SeckillConfigService seckillConfigService;
	
	@ModelAttribute
	public SeckillConfig get(@RequestParam(required=false) Long id) {
		if (id != null){
			return seckillConfigService.get(id);
		}else{
			return new SeckillConfig();
		}
	}
	
	@RequiresPermissions("wechat:seckillConfig:view")
	@RequestMapping(value = {"list", ""})
	public String list(SeckillConfig seckillConfig, HttpServletRequest request, HttpServletResponse response, Model model) {
		User user = UserUtils.getUser();
		if (!user.isAdmin()){
			seckillConfig.setUser(user);
		}
        Page<SeckillConfig> page = seckillConfigService.find(new Page<SeckillConfig>(request, response), seckillConfig); 
        model.addAttribute("page", page);
		return "modules/wechat/seckillConfigList";
	}

	@RequiresPermissions("wechat:seckillConfig:view")
	@RequestMapping(value = "form")
	public String form(SeckillConfig seckillConfig, Model model) {
		User user = UserUtils.getUser();
		seckillConfig.setUser(user);
		if(seckillConfig.getId() == null){
			SeckillConfig temp =seckillConfigService.getByUserId(user.getId());
			if(temp!=null){
				seckillConfig = temp;
			}
		}
		
		model.addAttribute("seckillConfig", seckillConfig);
		return "modules/wechat/seckillConfigForm";
	}

	@RequiresPermissions("wechat:seckillConfig:edit")
	@RequestMapping(value = "save")
	public String save(@RequestParam("topPicFile") MultipartFile topPicFile,@RequestParam("seckillPicFile") MultipartFile seckillPicFile,SeckillConfig seckillConfig, Model model,HttpServletRequest request,  RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, seckillConfig)){
			return form(seckillConfig, model);
		}
		
		//秒杀页面的图片
		if(topPicFile != null && topPicFile.getOriginalFilename().length()>4){
			String seckillPath = WechatUtils.getWechatSeckillResourceSavePath(request)+File.separator;
			FileUtils.createDirectory(seckillPath);
			String sname = topPicFile.getOriginalFilename();
			String fileType = sname.substring(sname.lastIndexOf("."));
			String fname  = UserUtils.getUser().getLoginName()+	"_seckill_"+System.currentTimeMillis()+fileType;
			File file = new File(seckillPath+fname);
			System.out.println("保存文件:"+seckillPath+fname);
			try {
				topPicFile.transferTo(file);
				seckillConfig.setTopPic(file.getName());
			} catch (Exception e) {
				System.out.println("上传文件失败:"+seckillPath+fname);
				e.printStackTrace();
			}
			
		}
		
		//秒杀消息导航的图片
		if(seckillPicFile != null && seckillPicFile.getOriginalFilename().length()>4){
			String seckillLinkPicPath = WechatUtils.getWechatSeckillLinkSavePath(request)+File.separator;
			FileUtils.createDirectory(seckillLinkPicPath);
			String sname = seckillPicFile.getOriginalFilename();
			String fileType = sname.substring(sname.lastIndexOf("."));
			String fname  = UserUtils.getUser().getLoginName()+	"_seckill_nav"+fileType;
			File file = new File(seckillLinkPicPath+fname);
			System.out.println("保存文件:"+seckillLinkPicPath+fname);
			try {
				seckillPicFile.transferTo(file);
				seckillConfig.setSeckillPic(file.getName());
			} catch (Exception e) {
				System.out.println("上传文件失败:"+seckillLinkPicPath+fname);
				e.printStackTrace();
			}
			
		}
		seckillConfig.setCostScore((int)(Double.parseDouble(request.getParameter("pCostScore"))*1000));
		seckillConfig.setMinScore((int)(Double.parseDouble(request.getParameter("pMinScore"))*1000));
		seckillConfigService.save(seckillConfig);
		addMessage(redirectAttributes, "保存秒杀配置'" + seckillConfig.getName() + "'成功");
		if(UserUtils.getUser().isAdmin()){
			
			return "redirect:"+Global.ADMIN_PATH+"/wechat/seckillConfig/?repage";
		}else{
			return form(seckillConfig, model);
		}
	}
	
	@RequiresPermissions("wechat:seckillConfig:edit")
	@RequestMapping(value = "delete")
	public String delete(Long id, RedirectAttributes redirectAttributes) {
		seckillConfigService.delete(id);
		addMessage(redirectAttributes, "删除秒杀配置成功");
		if(UserUtils.getUser().isAdmin()){
			
			return "redirect:"+Global.ADMIN_PATH+"/wechat/seckillConfig/?repage";
		}else{
			return form(null, null);
		}
	}

}
